
//Copyright 1997-2009 Syrinx Development, Inc.
//This file is part of the Syrinx Web Application Framework (SWAF).
// == BEGIN LICENSE ==
//
// Licensed under the terms of any of the following licenses at your
// choice:
//
//  - GNU General Public License Version 3 or later (the "GPL")
//    http://www.gnu.org/licenses/gpl.html
//
//  - GNU Lesser General Public License Version 3 or later (the "LGPL")
//    http://www.gnu.org/licenses/lgpl.html
//
//  - Mozilla Public License Version 1.1 or later (the "MPL")
//    http://www.mozilla.org/MPL/MPL-1.1.html
//
// == END LICENSE ==
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Diagnostics;

using Swaf;
using Swaf.BizObj;
using Swaf.BizRule;
using Swaf.Gui;
using Swaf.Gui.AspNet2;
using Swaf.Security;
using UserLogin;


namespace UserLogin.Gui
{
	public delegate void UserLoginBPHandler(string msg);

    public class UserLoginBizProc : BaseBusinessProcess
    {
        protected bool m_showUserLoginOptions;

		public UserLoginBizProc(ActiveBizProc procState)
			: base(procState)
		{
			if (m_currentBizObj == null)
				if (UserLoggedIn)
					m_currentBizObj = CurrentUser;
				else
					State.InProgressAddBizObj = App.bizObjMgr.create("User");
			updateActionState();
		}

		protected override void setupRuleNames()
		{
			m_updateBizObjRule = "loginUser";
			m_deleteBizObjRule = "logoffUser";
		}

		public override void updateActionState()
		{
			base.updateActionState();
			((GenericAction)Actions["DeleteBizObj"]).Availability = UserLoggedIn ? ActionAvailability.enabled : ActionAvailability.none;
			((GenericAction)Actions["UpdateBizObj"]).Availability = !UserLoggedIn ? ActionAvailability.enabled : ActionAvailability.none;

		}

		protected override Privileges CurrentEditPrivilege
		{
			get { return  !UserLoggedIn?Privileges.Delete:Privileges.ReadOnly; }
		}

		protected override bool validateBizObj(IBizObj obj)
		{
			return true;
		}

		public override IResults updateBizObj(IBizObj user)
		{
			IResults rc = base.updateBizObj(user);
			if (rc.ExceptionInfo != null)
				displayMessageFromBizRuleException("Error-CannotLogin",rc.ExceptionInfo);
			else
			{
				LoginResult lr = rc.CallResults as LoginResult;
				switch (lr.UserLoginResultInfo)
				{
					case UserLoginResult.InvalidUserPassword:
						m_procState.BPC.Messenger.add("Error-InvalidUserPassword", null);
						break;
					case UserLoginResult.UserAccountDisabled:
						m_procState.BPC.Messenger.add("Error-UserAccountDisabled", null);
						break;
					case UserLoginResult.UserCompanyDisabled:
						m_procState.BPC.Messenger.add("Error-UserCompanyDisabled", null);
						break;
					case UserLoginResult.ValidUserPassword:
						m_procState.BPC.Messenger.add("Info-ValidUserPassword", null);
						m_retrievedCurrentBizObj = true;
						m_currentBizObj = CurrentUser;
						forceChildBizProcUpdates();
						break;
				}
			}
			return rc;			
		}
		public bool UserLoggedIn
		{
			get { return (bool)CurrentUser.get("authenticated"); }
		}
	}
}
